环境准备
阿里云申请3台ECS,如下:
序号 |
内网IP |
OS |
A |
172.16.190.78 |
CentOS 7.6 |
B |
172.16.242.36 |
CentOS 7.6 |
C |
172.16.190.77 |
CentOS 7.6 |
节点规划
采用三主三从:
主节点 |
从节点 |
172.16.190.78:7000 |
172.16.242.36:7001 |
172.16.242.36:7000 |
172.16.190.77:7001 |
172.16.190.77:7000 |
172.16.190.78:7001 |
部署Redis
安装
在每台服务器上执行以下操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| $ cd ~/software # 下载redis安装包 $ wget http://download.redis.io/releases/redis-4.0.11.tar.gz $ tar -xf redis-4.0.11.tar.gz
#编译 $ cd redis-4.0.11 $ make
# 创建redis安装目录,并将可执行文件拷贝到bin下 $ mkdir -pv ~/redis/conf ~/redis/bin ~/redis/log ~/redis/data/7000 ~/redis/data/7001 $ cd ~/redis $ cp ../software/redis-4.0.11/src/redis-* bin/ $ rm bin/*.c *.o
|
配置
在每台机器上创建redis实例的配置,如在A节点上
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| # 配置7000端口实例 $ vim ~/redis/conf/redis7000.conf bind 172.16.190.78 port 7000 cluster-enabled yes cluster-config-file nodes.conf cluster-node-timeout 5000 appendonly yes timeout 3600 daemonize yes pidfile /home/dolphin/redis/pid/redis7000.pid logfile "/home/dolphin/redis/log/redis7000.log" dir "/home/dolphin/redis/data/7000" rename-command FLUSHALL "" rename-command FLUSHDB "" rename-command KEYS ""
# 配置7001端口实例 $ vim ~/redis/conf/redis7000.conf bind 172.16.190.78 port 7001 cluster-enabled yes cluster-config-file nodes.conf cluster-node-timeout 5000 appendonly yes timeout 3600 daemonize yes pidfile /home/dolphin/redis/pid/redis7001.pid logfile "/home/dolphin/redis/log/redis7001.log" dir "/home/dolphin/redis/data/7001" rename-command FLUSHALL "" rename-command FLUSHDB "" rename-command KEYS ""
|
其他两个节点类似。
启动实例
1 2 3
| $ cd ~/redis $ bin/redis-server conf/redis7000.conf $ bin/redis-server conf/redis7001.conf
|
创建集群
如果是redis 5,可以直接用redis-cli工具来创建集群。对于Redis 4或3,需要另一个工具 redis-trib.rb来帮忙。对于 redis-trib.rb,需要安装redis gem。
1 2 3 4 5 6 7
| $ gem install redis Fetching redis-4.1.4.gem Successfully installed redis-4.1.4 Parsing documentation for redis-4.1.4 Installing ri documentation for redis-4.1.4 Done installing documentation for redis after 1 seconds 1 gem installed
|
若报错误如下,需要升级ruby的版本,参考附录部分。
1 2 3
| Fetching: redis-4.1.4.gem (100%) ERROR: Error installing redis: redis requires Ruby version >= 2.3.0.
|
然后通过 redis-trib.rb 创建redis集群
1
| $ redis-trib.rb create --replicas 1 172.16.190.78:7000 172.16.242.36:7000 172.16.190.79:7000 172.16.190.78:7001 172.16.242.36:7001 172.16.190.79:7001
|
在弹出的确认中输入yes,一切顺利的话最终会出现如下提示
1
| [OK] All 16384 slots covered
|
此时代表 redis cluster已经部署成功。
验证
登陆任意节点
1 2 3 4 5 6 7 8 9
| $ redis-cli -h 72.16.190.78 -p 7000 -c 172.16.190.78:7000> cluster nodes 4a1b49cd77dbd18eba677663777f211be6f68dae 172.16.242.36:7001@17001 slave 2c75029ab638a48537a4c02ed0ca77a19fc4106b 0 1591274265000 5 connected d0381ef4aad364c42e08bf1c2d78168f4901bd90 172.16.190.78:7001@17001 slave 08a5e808d2e6f6b231d73519bd4f05f74614c2a2 0 1591274265000 4 connected 08a5e808d2e6f6b231d73519bd4f05f74614c2a2 172.16.190.77:7000@17000 master - 0 1591274266097 3 connected 10923-16383 e5c154dcf02526b807c67bebf0a63b4c98118ffe 172.16.190.77:7001@17001 slave 50884e234c5f1ccf03f5a6d1cc4e6e6dc4779752 0 1591274266197 6 connected 2c75029ab638a48537a4c02ed0ca77a19fc4106b 172.16.190.78:7000@17000 myself,master - 0 1591274264000 1 connected 0-5460 50884e234c5f1ccf03f5a6d1cc4e6e6dc4779752 172.16.242.36:7000@17000 master - 0 1591274265193 2 connected 5461-10922 172.16.190.78:7000>
|
第一列为节点id,可以看到节点172.16.190.78:7000为master,且分配的slot为0-5460,而172.16.242.36:7001为它的slave节点。
附录:升级rvm安装ruby
升级rvm的版本
1
| $ curl -sSL https://get.rvm.io | bash -s stable
|
root用户rvm下载的路径为/usr/local/rvm
1 2 3
| #解压rvm压缩包 $ cd /usr/local/rvm/archives $ tar -xf rvm-1.29.10.tgz
|
另起一个窗口
1 2 3 4
| $ cd /usr/local/rvm/archives/rvm-1.29.10/scripts $ source rvm # 修改rvm的ruby安装源,修改 RVM 的 Ruby 安装源到 Ruby China 的 Ruby 镜像服务器,这样能提高安装速度 $ echo "ruby_url=https://cache.ruby-china.org/pub/ruby" > /usr/local/rvm/user/db
|
查看ruby的版本,并安装
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| $ rvm list known # MRI Rubies [ruby-]1.8.6[-p420] [ruby-]1.8.7[-head] # security released on head [ruby-]1.9.1[-p431] [ruby-]1.9.2[-p330] [ruby-]1.9.3[-p551] [ruby-]2.0.0[-p648] [ruby-]2.1[.10] [ruby-]2.2[.10] [ruby-]2.3[.8] [ruby-]2.4[.9] [ruby-]2.5[.7] [ruby-]2.6[.5] [ruby-]2.7[.0] ruby-head
$ rvm install 2.4.9
|
安装成功后,默认使用的还是ruby老版本,需要指定使用新的版本。
验证当前ruby的版本
1 2
| $ ruby -version ruby 2.4.9p362 (2019-10-02 revision 67824) [x86_64-linux]
|
升级成功。